﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class AddCategory : System.Web.UI.UserControl
    {
        public int intactivetop, intactiveleft;
        public string pathToCreate;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Them du lieu vao cat 1
                try
                {
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", 0, "selectwhereid");
                    ListItem lt = new ListItem("Chon danh muc", "-1");
                    Cate1.Items.Add(lt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                        Cate1.Items.Add(lt);
                    }
                    Cate1.SelectedIndex = 0;
                }
                catch (Exception)
                {
                    Response.Redirect("/error.htm");
                }
            }
        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            try
            {
                Cate2.Items.Clear();
                Cate3.Items.Clear();
                int index = Cate1.SelectedIndex;
                if (index > 0)
                {
                    int id = Convert.ToInt32(Cate1.SelectedValue);
                    // Load du lieu
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
                    ListItem lt = new ListItem("Chon danh muc", "-1");
                    Cate2.Items.Add(lt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                        Cate2.Items.Add(lt);
                    }
                    txtNameCategory.Enabled = false;
                    fulImageCategory.Enabled = false;
                    CKEditorMota.Enabled = false;
                    ddlActiveLeft.Enabled = false;
                    ddlActiveTop.Enabled = false;
                    btnAddCategory.Enabled = false;
                    Cate2.Enabled = true;
                    btnCate2.Enabled = true;
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            try
            {
                Cate3.Items.Clear();
                int index = Cate2.SelectedIndex;

                int id = Convert.ToInt32(Cate2.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate3.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate3.Items.Add(lt);
                }
                txtNameCategory.Enabled = false;
                fulImageCategory.Enabled = false;
                CKEditorMota.Enabled = false;
                ddlActiveLeft.Enabled = false;
                ddlActiveTop.Enabled = false;
                btnAddCategory.Enabled = false;
                Cate3.Enabled = true;
                btnCate3.Enabled = true;
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnAddCate1_Click(object sender, EventArgs e)
        {
            txtNameCategory.Enabled = true;
            fulImageCategory.Enabled = true;
            CKEditorMota.Enabled = true;
            ddlActiveLeft.Enabled = true;
            ddlActiveTop.Enabled = true;
            btnAddCategory.Enabled = true;
            lblIndex.Text = "1";
            lbldanhmuc.Text = "Bạn đang thêm danh mục cấp 1";


        }

        protected void btnCate2_Click(object sender, EventArgs e)
        {

            try
            {
                int id = Convert.ToInt32(Cate1.SelectedValue);
                DataTable dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "SELECT count(*) as result FROM tbl_Product WHERE Category_ID3 = " + id + "", true);
                if (dt.Rows[0]["result"].ToString() == "0")
                {
                    txtNameCategory.Enabled = true;
                    fulImageCategory.Enabled = true;
                    CKEditorMota.Enabled = true;
                    ddlActiveLeft.Enabled = true;
                    ddlActiveTop.Enabled = true;
                    btnAddCategory.Enabled = true;
                    btnCate2.Enabled = true;
                    lblIndex.Text = "2";
                    lbldanhmuc.Text = "Bạn đang thêm danh mục cấp 2";
                    lblmenunngang.Visible = false;
                    lblmenutrai.Visible = false;
                    ddlActiveLeft.Visible = false;
                    ddlActiveTop.Visible = false;
                }
                else
                {
                    txtNameCategory.Enabled = false;
                    fulImageCategory.Enabled = false;
                    CKEditorMota.Enabled = false;
                    ddlActiveLeft.Enabled = false;
                    ddlActiveTop.Enabled = false;
                    btnAddCategory.Enabled = false;
                    Cate2.Enabled = false;
                    btnCate2.Enabled = true;
                    lblmenunngang.Visible = false;
                    lblmenutrai.Visible = false;
                    ddlActiveLeft.Visible = false;
                    ddlActiveTop.Visible = false;
                    Response.Write("<script>alert('Danh mục 1 đã có sản phẩm . Bạn không thể thêm danh mục 2')</script>");
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnCate3_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(Cate2.SelectedValue);
                DataTable dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "SELECT count(*) as result FROM tbl_Product WHERE Category_ID3 = " + id + "", true);
                if (dt.Rows[0]["result"].ToString() == "0")
                {
                    txtNameCategory.Enabled = true;
                    fulImageCategory.Enabled = true;
                    CKEditorMota.Enabled = true;
                    ddlActiveLeft.Enabled = true;
                    ddlActiveTop.Enabled = true;
                    btnAddCategory.Enabled = true;
                    btnCate3.Enabled = true;
                    lblIndex.Text = "3";
                    lblmenunngang.Visible = false;
                    lblmenutrai.Visible = false;
                    ddlActiveLeft.Visible = false;
                    ddlActiveTop.Visible = false;
                    lbldanhmuc.Text = "Bạn đang thêm danh mục cấp 3";
                }
                else
                {
                    txtNameCategory.Enabled = false;
                    fulImageCategory.Enabled = false;
                    CKEditorMota.Enabled = false;
                    ddlActiveLeft.Enabled = false;
                    ddlActiveTop.Enabled = false;
                    btnAddCategory.Enabled = false;
                    Cate3.Enabled = false;
                    btnCate3.Enabled = true;
                    lblmenunngang.Visible = false;
                    lblmenutrai.Visible = false;
                    ddlActiveLeft.Visible = false;
                    ddlActiveTop.Visible = false;
                    Response.Write("<script>alert('Danh mục 2 đã có sản phẩm . Bạn không thể thêm danh mục 3')</script>");
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnAddCategory_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Category", "Category_ID1"));
            int nextCategory_Ordinal = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Category", "Category_Ordinal"));
            string filePathIamgeCate = null;
            string cate1;
            string cate2;
            string activetop = ddlActiveTop.Text;
            string activeleft = ddlActiveLeft.Text;
            if (lblIndex.Text.Equals("1"))
            {
                if (activetop == "Có")
                {
                    intactivetop = 1;
                }
                else
                {
                    intactivetop = 0;
                }
                if (activeleft == "Có")
                {
                    intactiveleft = 1;
                }
                else
                {
                    intactiveleft = 0;
                }

                if (txtNameCategory.Text != "")
                {
                    if (fulImageCategory.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageCategory.FileName))
                        {
                            DataTable checkcate = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Select count(*) as result from tbl_Category Where Category_Name = N'" + txtNameCategory.Text + "' AND Category_ID2 = 0", true);

                            if (checkcate.Rows[0]["result"].ToString() != "0")
                            {
                                Response.Write("<script>alert('Danh mục 1 này đã tồn tại...')</script>");
                            }
                            else
                            {
                                int idcate = nextid + 1;
                                int idshot = nextCategory_Ordinal + 1;
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathIamgeCate = "/Images/Image-Category/" + fulImageCategory.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Category/"), fulImageCategory.FileName, 200, fulImageCategory.PostedFile.InputStream);
                                string value = "" + idcate + ",0,N'" + txtNameCategory.Text + "','" + filePathIamgeCate + "','" + CKEditorMota.Text + "','" + intactivetop + "','" + intactiveleft + "'," + idshot + "";
                                List<string> values = new List<string>();
                                values.Add(value);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Category", "insertemployee", values, false);
                                Response.Redirect("/Admin/Index.aspx?panel=2&sub=0");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Vui lòng upload ảnh danh mục...')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập tên danh mục...')</script>");
                }

            }

            if (lblIndex.Text.Equals("2"))
            {
                if (txtNameCategory.Text != "")
                {
                    if (fulImageCategory.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageCategory.FileName))
                        {
                            if (nextid == 0)
                            {
                                cate1 = Cate1.Text;
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathIamgeCate = "/Images/Image-Category/" + fulImageCategory.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Category/"), fulImageCategory.FileName, 200, fulImageCategory.PostedFile.InputStream);
                                string value = "1,'" + cate1 + "',N'" + txtNameCategory.Text + "','" + filePathIamgeCate + "','" + CKEditorMota.Text + "',1,1,1";
                                List<string> values = new List<string>();
                                values.Add(value);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Category", "insertemployee", values, false);
                                Response.Redirect("/Admin/Index.aspx?panel=2&sub=0");
                            }
                            else
                            {
                                int idcate = nextid + 1;
                                cate1 = Cate1.Text;
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathIamgeCate = "/Images/Image-Category/" + fulImageCategory.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Category/"), fulImageCategory.FileName, 200, fulImageCategory.PostedFile.InputStream);
                                string value = "" + idcate + ",'" + cate1 + "',N'" + txtNameCategory.Text + "','" + filePathIamgeCate + "','" + CKEditorMota.Text + "',1,1,1";
                                List<string> values = new List<string>();
                                values.Add(value);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Category", "insertemployee", values, false);
                                Response.Redirect("/Admin/Index.aspx?panel=2&sub=0");
                            }

                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Vui lòng upload ảnh danh mục...')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhạp tên danh mục...')</script>");
                }
            }

            if (lblIndex.Text.Equals("3"))
            {
                if (txtNameCategory.Text != "")
                {
                    if (fulImageCategory.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageCategory.FileName))
                        {
                            if (nextid == 0)
                            {
                                cate2 = Cate2.Text;
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathIamgeCate = "/Images/Image-Category/" + fulImageCategory.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Category/"), fulImageCategory.FileName, 200, fulImageCategory.PostedFile.InputStream);
                                string value = "1,'" + cate2 + "',N'" + txtNameCategory.Text + "','" + filePathIamgeCate + "','" + CKEditorMota.Text + "',1,1,1";
                                List<string> values = new List<string>();
                                values.Add(value);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Category", "insertemployee", values, false);
                                Response.Redirect("/Admin/Index.aspx?panel=2&sub=0");
                            }
                            else
                            {
                                int idcate = nextid + 1;
                                cate2 = Cate2.Text;
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathIamgeCate = "/Images/Image-Category/" + fulImageCategory.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Category/"), fulImageCategory.FileName, 200, fulImageCategory.PostedFile.InputStream);
                                string value = "" + idcate + ",'" + cate2 + "',N'" + txtNameCategory.Text + "','" + filePathIamgeCate + "','" + CKEditorMota.Text + "',1,1,1";
                                List<string> values = new List<string>();
                                values.Add(value);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Category", "insertemployee", values, false);
                                Response.Redirect("/Admin/Index.aspx?panel=2&sub=0");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Vui lòng upload ảnh danh mục...')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập tên danh mục...')</script>");
                }
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
        }
    }
}